<?php

namespace App\Imports\PolicyBatchUpload\QnPolicy;

use App\Repositories\Contracts\QnPolicyInterface;
use App\Repositories\Ota\Qn\Policy\Contracts\QuPolicyInterface as QnOtaPolicyInterface;
use Curl\StrService\StrService;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class RoundBatchUpload implements ToCollection
{
    public $uploadResult = false;

    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {

        $adminId = request()->admin['id'];
        $isDel   = request()->delPolicy == 1 ? true : false;
        $policyUploads = [];

        $policies = [];

        $ext = StrService::uuid();
        //$callbackUrl =  env('APP_URL').'/api/qn/notify';
        $callbackUrl = 'http://42.229.47.193:18081/cb/18804/api/qn/notify';
        unset($collection[0]);
        foreach ($collection as $row) {

            if (empty($row[0])) {

                break;
            }

            $policyUploads[] = [
                'admin_id'                  => $adminId,
                'policy_code'               => $row[0],
                'airline'                   => $row[2],
                'depart_city_code'          => $row[3],
                'arrived_city_code'         => $row[4],
                'flight_code'               => $row[10],
                'depart_at'                 => $row[12],
                'cabin'                     => $row[7],
                'price'                     => $row[45],
                'ext'                       => $ext,
                'platform'                  => 32,
                'status'                    => $row[1] == '有效' ? 1 : 0,
                'created_at'                => date('Y-m-d H:i:s'),
                'updated_at'                => date('Y-m-d H:i:s'),
                'remark'                    => $row[82],
                'upload_msg'                => '等待确认上传结果'
            ];

            $policies[] = [
                'policyCode'                        => $row[0],
                'policycode'                        => $row[0],
                'status'                            => $row[1],
                'carrier'                           => $row[2],
                'dpt'                               => $row[3],
                'arr'                               => $row[4],
                'dpt2'                              => $row[5],
                'arr2'                              => $row[6],
                'cabin'                             => $row[7],
                'forbidCabin'                       => $row[8],
                'flightNoLimit'                     => $row[9],
                'flightCondition'                   => $row[10],
                'shared'                            => $row[11],
                'dptDateStart'                      => $row[12],  //时间需要调整
                'dptDateEnd'                        => $row[13], //时间需要调整
                'dptDateForbid'                     => $row[14],
                'deptTimeSlot'                      => $row[15],
                'carrier2'                          => $row[16],
                'dayCondition'                      => $row[17],
                'cabin2'                            => $row[18],
                'forbidCabin2'                      => $row[19],
                'flightNoLimit2'                    => $row[20],
                'flightCondition2'                  => $row[21],
                'shared2'                           => $row[22],
                'dptDateStart2'                     => $row[23],
                'dptDateEnd2'                       => $row[24],
                'dptDateForbid2'                    => $row[25],
                'deptTimeSlot2'                     => $row[26],
                'dayCondition2'                     => $row[27],
                'forbidAirlines'                    => $row[28],
                'supportMultiAirports'              => $row[29],
                'productMark'                       => $row[30],
                'saleDateStart'                     => $row[31],
                'saleDateEnd'                       => $row[32],
                'saleDateForbidStart'               => $row[33],
                'saleDateForbidEnd'                 => $row[34],
                'saleTimeSlot'                      => $row[35],
                'lockTime'                          => $row[36],
                'shortStopTime'                     => $row[37],
                'longStopTime'                      => $row[38],
                'earliestBeforeTicket'              => $row[39],
                'beforeTicket'                      => $row[40],
                'earliestBeforeTicket2'             => $row[41],
                'beforeTicket2'                     => $row[42],
                'cardType'                          => $row[43],
                'printSource'                       => $row[44],
                'printPrice'                        => $row[45],
                'selectType'                        => $row[46],
                'printComputeType'                  => $row[47],
                'xCabin'                            => $row[48],
                'linkedCabin'                       => $row[49],
                'printReturnPoint'                  => $row[50],
                'printReturnPrice'                  => $row[51],
                'xPriceType'                        => $row[52],
                'minPrintPrice'                     => $row[53],
                'maxPrintPrice'                     => $row[54],
                'policyReturnPoint'                 => $row[55],
                'policyReturnPrice'                 => $row[56],
                'printSource2'                      => $row[57],
                'printPrice2'                       => $row[58],
                'selectType2'                       => $row[59],
                'printComputeType2'                 => $row[60],
                'xCabin2'                           => $row[61],
                'linkedCabin2'                      => $row[62],
                'printReturnPoint2'                 => $row[63],
                'printReturnPrice2'                 => $row[64],
                'xPriceType2'                       => $row[65],
                'minPrintPrice2'                    => $row[66],
                'maxPrintPrice2'                    => $row[67],
                'policyReturnPoint2'                => $row[68],
                'policyReturnPrice2'                => $row[69],
                'autoPriceThreshold'                => $row[70],
                'percentPriceThreshold'             => $row[71],
                'preOfficeNo'                       => $row[72],
                'needPnr'                           => $row[73],
                'officeNo'                          => $row[74],
                'needPata'                          => $row[75],
                'returnRule'                        => $row[76],
                'changeRule'                        => $row[77],
                'endorsement'                       => $row[78],
                'returnRule2'                       => $row[79],
                'changeRule2'                       => $row[80],
                'endorsement2'                      => $row[81],
                'cabinNote'                         => $row[82],
                'highDemandType'                    => $row[83],
                'highDemandThreshold'               => $row[84],
                'type'                              => 'ROUND',
                'ext'                               => $ext,
                'url'                               => $callbackUrl,
            ];

        }

        if (!empty($policies)) {
            $qnOtaPolicyInterface = resolve(QnOtaPolicyInterface::class);
            $res = $qnOtaPolicyInterface->add($policies,$isDel);

            if (substr($res,0,1) == "\n") {

                //去哪上传政策
                $qnPolicyInterface = resolve(QnPolicyInterface::class);

                //删除库内政策
                if ($isDel) {

                    $delWhere = [
                        'admin_id' => $adminId,
                        [ 'policy_code','like',$policyUploads[0]['policy_code'],]
                    ];
                    $policyUploadsRes = $qnPolicyInterface->delete($delWhere);
                }

                $policyUploadsRes = $qnPolicyInterface->insert($policyUploads);
                if ($policyUploadsRes) {

                    $this->uploadResult = true;
                }
            }
        }

    }
}
